# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from article.models import Article
from datetime import datetime
import logging
import traceback
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

# Get an instance of a logger
logger = logging.getLogger(__name__)

# Create your views here.
def home(request):
    posts = Article.objects.all()  #获取全部的Article对象
    paginator = Paginator(posts, 10) #每页显示两个
    page = request.GET.get('page')
    try :
        post_list = paginator.page(page)
    except PageNotAnInteger :
        post_list = paginator.page(1)
    except EmptyPage :
        post_list = paginator.paginator(paginator.num_pages)
    return render(request, 'home.html', {'post_list' : post_list})


def detail(request, id=''):
    try:
        post = Article.objects.all()[int(id)]    
        print("id is ",id)
        article = Article.objects.get(id = id)
        str = ("title = %s, category = %s, date_time = %s, content = %s" 
            % (post.title, post.category, post.date_time, post.content))
        return render(request,'detail.html',{'article' :article})
    except Exception,e:
        logger.error('Something went wrong!  %s' % e)
        print(e)
